package com.woniu.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.info.entity.MessageDTO;
import com.woniu.info.pojo.Message;
import com.woniu.info.mapper.MessageMapper;
import com.woniu.info.service.IMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 消息通知表 服务实现类
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {
    @Autowired(required = false)
    private MessageMapper messageMapper;
    @Autowired(required = false)
    private RabbitTemplate rabbitTemplate;

    @Override
    public void addMessage(Message message) {
        int insert = messageMapper.insert(message);
        System.out.println(message.getTargetType().equals("0"));
        if(message.getTargetType().equals("0") ){
            rabbitTemplate.convertAndSend("message_exchange","add_message_shop","新增消息类别：" + message.getMessageType() + "  ， 请前往门店端进行查看！");
        }else{
            rabbitTemplate.convertAndSend("message_exchange","add_message_user","新增消息类别：" + message.getMessageType() + "  ， 请前往用户端进行查看！");
        }
    }

    @Override
    public void deleteMessage(String id) {
        int deleteById = messageMapper.deleteById(id);
    }

    @Override
    public void updateMessage(Message message) {
        int updateById = messageMapper.updateById(message);
    }

    @Override
    public PageResult<Message> queryMessage(Integer currentPage, Integer pageSize, MessageDTO messageDTO) {
        IPage<Message> page = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(messageDTO.getMessageType()),Message::getMessageType,messageDTO.getMessageType())
                .like(!StringUtils.isEmpty(messageDTO.getTargetType()),Message::getTargetType,messageDTO.getTargetType());
        IPage<Message> selectPage = messageMapper.selectPage(page, wrapper);
        return new PageResult<>(selectPage.getTotal(),selectPage.getRecords());
    }
}
